package com.banshuibao.bsb_android.fragment;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.O2oAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.O2oModel;
import com.banshuibao.bsb_android.model.RequestBodyModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;
import com.banshuibao.bsb_android.util.IntentManager;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class O2OFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener2, AdapterView.OnItemClickListener, View.OnClickListener {

    private PullToRefreshListView pullToRefreshListView;

    private ListView listView;

    private O2oAdapter adapter;

    private int startId,endId;

    private boolean isUp = false;

    private int row;

    public O2OFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_o2_o, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
       initView(view);
       request();
    }
    public void initView(View view){
       ImageView search = getActivity().getWindow().getDecorView().findViewById(R.id.search);
       search.setOnClickListener(this);
        pullToRefreshListView = view.findViewById(R.id.ptrlv);
        pullToRefreshListView.setOnRefreshListener(this);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        listView = pullToRefreshListView.getRefreshableView();
        adapter = new O2oAdapter(getActivity());
        listView.setOnItemClickListener(this);
        listView.setAdapter(adapter);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        request();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        isUp = true;
        endId = startId+9;
        getData();
    }

    public void request(){
        isUp = false;
        startId = 1;
        endId = startId+9;
        if(adapter!=null){
            int count = adapter.getCount();
            if(count>10){
                endId += count-10;
            }
        }
        getData();
    }

    public void getData(){
        RequestBodyModel requestBodyModel = new RequestBodyModel();
        requestBodyModel.setSinceId(startId);
        requestBodyModel.setMaxId(endId);
        HttpUtil.doPost(Constants.Url.COUNSELOR_LIST, requestBodyModel, new HttpResponse(getActivity(), O2oModel.class) {
            @Override
            public void onResult(Object obj) {
               O2oModel model = (O2oModel) obj;
                List<O2oModel.DataBean> data = model.getData();
                row = model.getRows();
                startId += row;
                if(isUp){
                    adapter.setUpData(data);
                }else{
                    adapter.setDownData(data);
                }

            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullToRefreshListView.onRefreshComplete();
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(adapter!=null){
            O2oModel.DataBean dataBean = adapter.getItem(position - 1);
            if(dataBean!=null){
                IntentManager.goPersonPageActivity(getActivity(),dataBean.getUuid());
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search:
                IntentManager.goSearchPersonActivity(getActivity());
                break;
        }
    }
}
